#include <htc.h>
#include"ADC.h";

void adc_init()
{
// ADC conversion clock: = Fosc/8
//            ADCS <1:0> = 00    Fosc/2
//            ADCS <1:0> = 01    Fosc/8
//            ADCS <1:0> = 10    Fosc/32
//            ADCS <1:0> = 11    F_RC
	ADCS1 = 0;
	ADCS0 = 1;
// Voltage reference: Internal Vref
//            VCFG <1:0> = 00    Internal Vref
//            VCFG <1:0> = 11    External Vref
	VCFG1 = 0;
	VCFG0 = 0;
// Select Input chanel: AN3
//            CHS <3:0> = 0000    Select AN0
//            CHS <3:0> = 0001    Select AN1
//            CHS <3:0> = 0010    Select AN2
//             ...
//            CHS <3:0> = 1101    Select AN13
	CHS3 = 0;
	CHS2 = 0;
	CHS1 = 0;
	CHS0 = 1;
// Result format: Right
//                ADFM = 1 Right justified
//                ADFM = 0 Left justified
	ADFM = 1;
// Turn on ADC Module
	ADON = 1;
//delay to wait for adc module init.
	__delay_ms (1);
}
//----------------------------------------------------------------
//interrupt initialization
void intADC_init()
{
	GIE = 1;    //Global Interrupt Enable
	PEIE = 1;    //Peripheral Interrupt Enable
	ADIE = 1;    //ADC Interrupt Enable
	ADIF = 0;    //Clear ADC Interrupt Flag
}